package com.example.yls.demoa;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import java.sql.Connection;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
   private Button startservice;
   private Button stopservice;
   private Button bindservice;
   private Button disbindservice;
   private ServiceConnection serviceConnection;
   private MyBind myBind;
    private static final String TAG = "MainActivity";
    private Button btnadd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         initBundler();

         startserviceEvent();
         stopserviceEvent();
         bindserviceEvent();
         btnaddEvent();


    }

    private void btnaddEvent() {
        btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int result=myBind.sum(8,2);
                Log.e(TAG, "btnaddEvent: "+result);
            }
        });


    }

    private void bindserviceEvent() {
        bindservice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                bindService(intent,serviceConnection,BIND_AUTO_CREATE);
            }
        });

    }

    private void stopserviceEvent() {
        stopservice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                stopService(intent);

            }
        });


    }

    private void startserviceEvent() {
             startservice.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     Intent intent=new Intent();
                     intent.setClass(MainActivity.this,MyService.class);
                     startService(intent);
                 }
             });


    }

    private void initBundler() {
          startservice=findViewById(R.id.start_service);
          stopservice=findViewById(R.id.stop_service);
          btnadd=findViewById(R.id.btn_add);
          bindservice=findViewById(R.id.bind_service);
          serviceConnection=new ServiceConnection() {
              @Override
              public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                  Log.e(TAG, "onServiceConnected: ");
                  myBind= (MyBind) iBinder;
              }

              @Override
              public void onServiceDisconnected(ComponentName componentName) {

              }
          };

    }


}
